package com.gl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gl.entity.UcenterMember;
import com.gl.mapper.UcenterMemberMapper;
import com.gl.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gl.utils.JwtUtils;
import com.gl.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    //登录    返回jwt规则生成封装用户信息的token字符串
    @Override
    public String login(UcenterMember member) {

        String mobile = member.getMobile();//手机号
        String password = member.getPassword();//密码

        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new RuntimeException("手机号或密码不能为空");
        }

        //登录
        //判断手机号在数据库中是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember==null){
            throw new RuntimeException("没有这个手机号");
        }

        //判断密码是否正确
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new RuntimeException("密码错误");
        }

        //判断用户是否禁用
        if(ucenterMember.getIsDisabled()){
            throw new RuntimeException("用户已禁用");
        }
        //根据id和昵称生成token字符串
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());


        return token;
    }
    //判断数据库表里面是否存在相同微信信息，根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }
}
